import requests
import json

# 接口配置
url = "http://10.10.252.225:8000/v1-openai/embeddings"
headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer gpustack_bfc3cb4ed0c71e24_3838bb6deaa3c856a3f39b6f9d5af6c6"
}
payload = {
    "model": "bge-m3",
    "encoding_format": "float",
    "input": ["提供三分文档示例"]  # 支持多文本，例如 ["文本1", "文本2"]
}

try:
    # 发送POST请求
    response = requests.post(url, headers=headers, json=payload)
    response.raise_for_status()  # 检查HTTP错误

    # 解析响应
    result = response.json()
    print("请求成功！响应数据：")
    # print(json.dumps(result, indent=2, ensure_ascii=False))

    # 提取向量示例（假设响应结构与OpenAI兼容）
    if "data" in result and len(result["data"]) > 0:
        embedding = result["data"][0]["embedding"]
        print(f"\n文本向量维度: {len(embedding)}")
        print(f"前5维值: {embedding[:5]}")  # 打印前5维避免刷屏

except requests.exceptions.RequestException as e:
    print(f"请求失败: {e}")
except json.JSONDecodeError:
    print("错误：响应不是有效的JSON格式")
except KeyError:
    print("错误：响应结构不符合预期")